package org.voyager.common.beans;

import java.util.HashMap;
import java.util.Map;

import org.voyager.common.constants.DicCodes;

/**
 * 返回结果对象
 */
public class JsonResult extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	private JsonResult() {
	}

	/**
	 * 返回成功
	 */
	public static JsonResult ok() {
		return ok("操作成功");
	}

	/**
	 * 返回成功
	 */
	public static JsonResult ok(String message) {
		return ok(DicCodes.OK, message);
	}

	/**
	 * 返回成功
	 */
	public static JsonResult ok(int code, String message) {
		JsonResult jsonResult = new JsonResult();
		jsonResult.put("code", code);
		jsonResult.put("msg", message);
		return jsonResult;
	}

	/**
	 * 返回成功
	 */
	public static JsonResult ok(int code, String message, Object data) {
		JsonResult jsonResult = new JsonResult();
		jsonResult.put("code", code);
		jsonResult.put("msg", message);
		jsonResult.put("data", data);
		return jsonResult;
	}

	/**
	 * 返回失败
	 */
	public static JsonResult error() {
		return error("操作失败");
	}

	/**
	 * 返回失败
	 */
	public static JsonResult error(String messag) {
		return error(DicCodes.ERROR, messag);
	}

	/**
	 * 返回失败
	 */
	public static JsonResult error(int code, String message) {
		return ok(code, message);
	}

	/**
	 * 返回失败
	 */
	public static JsonResult error(int code, String message, Object data) {
		return ok(code, message, data);
	}

	/**
	 * 设置code
	 */
	public JsonResult setCode(int code) {
		super.put("code", code);
		return this;
	}

	/**
	 * 设置message
	 */
	public JsonResult setMessage(String message) {
		super.put("msg", message);
		return this;
	}

	/**
	 * 放入data
	 */
	public JsonResult putData(String key, Object object) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(key, object);
		super.put("data", result);
		return this;
	}

	/**
	 * 放入object
	 */
	@Override
	public JsonResult put(String key, Object object) {
		super.put(key, object);
		return this;
	}
}